Oops Basics

  • Note

    1. What is oops

    1. OOPS stands for Object oriented programming structure.

    2. It helps the developer to think and organize the code in terms of real world objects

    3. It also helps to design the code more modular, flexible, reusable and maintainable.

    4. OOPs groups related data and functions into classes

    2. Explain with a example

    In hospital management, "hospital", "doctor", "patient", "treatment", and "medicines" are real world objects. Each object have its own properties and behaviors.

    When developing a hospital management application, developer create classes for each real world objects and define the attributes and methods based on its properties and behaviors

    3. What is the use of Oops OR why should use oops?

    To design modular, flexible, reusable and maintainable code

    4. OOPS features

    1. Class

    Class is a blueprint of real world objects. It has attributes (member variables) and methods (member functions)

    2. Object

    Object is an instance of a class.

    When creating a instance, memory is allocated for the attributes in the object

    Each object has seperate memory location to store its own data

    3. Inheritance (Code Reusability)

    Common code can be written in a base (parent) class and reused in child classes.

    4. Polymorphism (Code maintainablity )

    You can use the same method name in parent and child classes with different behaviors.

    5. Encapsulation ( Security)

    1. Hides internal data.

    2. Keeps data safe from outside of the class using private and protected access modifiers .

    3. Helps to prevent accidental changes.

    6. Abstraction

    Hides internal logic and shows only relevant details using abstract classes or interfaces.

    Summary

    FeaturesUsagecode implementation
    ClassTo design properties and methods of an objectkeyword "class"
    ObjectTo store datakeyword "new"
    InheritanceShare the properties and logic to child classkeyword "extends"
    EncapsulationHide the dataaccess modifier "private" , "protected"
    PolymorphismRedefine the function logickeyword "extends" and redefine the function in child class
    AbstractionHide internal logicAbstract class & interface
    1. Abstract class vs interface

    Both are used for keeping common structure to the child classes

    Abstract class has shared properties and shared logics. But interface don't have properties and shared logics

    2. Static and non static variables

    Static variable is single copy for all objects. but non-static variables are seperate copy for each objects

    3. static methods vs non-static methods

    in static method, member variables are not accessible. but in non-static method, member variables are accessible